Questions
23 of 45
1What is a pseudo-element in CSS?
2How are pseudo-elements different from pseudo-classes?
3What is the syntax of a pseudo-element?
4Name some commonly used pseudo-elements.
5What does the ::before pseudo-element do? What does the ::after pseudo-element do?
6How can you insert content using pseudo-elements?
7What is the difference between :before and ::before? Why were pseudo-elements changed from one colon (:) to two (::) in CSS3?
8What does the ::first-letter pseudo-element do?
9What does the ::first-line pseudo-element do?
10How can you style the first line of a paragraph differently?
11Can pseudo-elements be styled with animations or transitions?
12What’s the difference between using ::before and ::after?
13Can pseudo-elements be positioned using CSS positioning properties? Can pseudo-elements have background images or colors? How can you use pseudo-elements to create shapes or icons?
14Can you use multiple pseudo-elements on the same element?
15What is the ::selection pseudo-element used for?
16How does stacking and z-index affect pseudo-elements?
17Can pseudo-elements have child elements or content inside them?
18Can you apply pseudo-elements to replaced elements (like <img>)?
19How do pseudo-elements interact with display and overflow properties?
20What’s the difference between using pseudo-elements and real elements for decoration?
21How does inheritance work with pseudo-elements?
22Can pseudo-elements trigger JavaScript events (like click)?
23How can you control the generated content with pseudo-elements using attr()?
24Can pseudo-elements be used inside flex or grid layouts?
25How do you control the stacking order of ::before and ::after pseudo-elements?
26How can you create a tooltip using pseudo-elements?
27How can you make a custom underline or highlight effect using ::after?
28How can you make a quotation mark appear before a paragraph using pseudo-elements?
29How do you create a triangle or arrow shape using only CSS pseudo-elements?
30How can you use pseudo-elements to add icons without extra markup?
31How can you create a button hover animation using ::before or ::after?
32How can pseudo-elements help in implementing skeleton loaders or shimmer effects?
33How can you use ::before and ::after to clear floats?
34How can you create a border animation using pseudo-elements?
35How can pseudo-elements be used in responsive design?
36What is the ::marker pseudo-element and when is it used?
37What does the ::placeholder pseudo-element do?
38How does ::cue work with media elements?
39What is ::backdrop, and where is it used?
40What is the difference between ::file-selector-button and other input pseudo-elements?
41How does the ::part() pseudo-element work in Web Components?
42How is ::slotted() different from ::part() in shadow DOM styling?
43Can you style text highlights with pseudo-elements?
44How can ::selection be customized for accessibility and branding?
45What are the limitations of pseudo-elements compared to actual DOM elements?
23 / 45

How can you control the generated content with pseudo-elements using attr()?

Using attr() with Pseudo-Elements in CSS

The CSS attr() function allows pseudo-elements like ::before and ::after to display the value of an HTML attribute dynamically. This enables you to generate content based on the element's attributes without modifying the HTML.

Key Points
  1. 1

    Use content: attr(attribute-name); to insert the value of a specific attribute.

  2. 2

    Only string-based attributes can be displayed using attr(). Numeric or URL values can also be used with certain properties like content.

  3. 3

    The attr() function can be combined with other text or symbols in the content property, e.g., content: 'Note: ' attr(data-note);.

  4. 4

    This method is ideal for tooltips, labels, counters, or displaying metadata dynamically.

Example: Displaying Data Attribute with ::after

In this example, the ::after pseudo-element reads the data-source attribute of the paragraph and displays it in parentheses after the text. You can change the attribute in HTML, and the pseudo-element will automatically reflect the new value.

Best Practices
  1. 1

    Use attr() for dynamically generated text content without extra markup.

  2. 2

    Combine with CSS styling like color, font-weight, or text-transform for visual emphasis.

  3. 3

    Avoid using pseudo-elements with attr() for interactive or functional content that requires JavaScript events.

  4. 4

    Test across browsers; basic attr() support for content is widely supported, but other properties may have limited compatibility.

Difficulty: 6/10
Topics: pseudo-elements, attr() function, dynamic content in CSS

Scenario Questions

0-2 years experience
  1. 1

    How would you use attr() and a pseudo-element to display a tooltip text from a data-tooltip attribute on hover?

  2. 2

    What happens if you try to use attr(data-count) to show a number inside a ::before pseudo-element on a button — will it render correctly?

2-5 years experience
  1. 1

    A tooltip built with attr() and ::after is showing up blank in production — the HTML has the attribute, but the CSS isn't rendering it. What would you check first?

  2. 2

    You're building a reusable card component that uses attr() to inject a category label via ::before. The design team wants to support multi-language labels. Is attr() a good solution here? Why or why not?

5-8 years experience
  1. 1

    You're designing a high-performance accessibility layer that uses attr() to inject ARIA labels via pseudo-elements. What are the performance and SEO implications of relying on CSS for dynamic content instead of the DOM?

  2. 2

    A legacy component uses attr() to render dynamic pricing in ::after, but now we need to support real-time price updates. How would you evaluate whether to keep this pattern or refactor to JS?

8+ years experience
  1. 1

    We're migrating a large UI library from inline JS-generated tooltips to a CSS-only solution using attr(). What architectural risks does this introduce for internationalization, accessibility compliance, and component reusability across teams?

  2. 2

    How would you design a system-wide policy around using attr() in pseudo-elements to avoid technical debt, especially when teams might use it as a shortcut instead of proper semantic HTML?

Follow-up Questions

  • What happens if the attribute you're referencing with attr() doesn't exist on the element?
  • Can you use attr() to pull in numeric values for calculations like width or font-size?
  • How would you debug a case where the pseudo-element content isn't showing up even though the attribute is present?